package heima3_2.cn.osc.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.bm.library.Info;
import com.bm.library.PhotoView;
import com.squareup.picasso.Picasso;

import net.oschina.app.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import heima3_2.cn.osc.constant.Constants;
import heima3_2.cn.osc.model.bean.DynamicFragmentBean.TweetComment;
import heima3_2.cn.osc.model.bean.DynamicFragmentBean.TweetCommentList;
import heima3_2.cn.osc.model.bean.DynamicFragmentBean.TweetDetail;
import heima3_2.cn.osc.model.re_interface.RetrofitInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

/**
 * Created by 超能浣熊 on 2017/1/17.
 */

public class DynamicDetailActivity extends Activity {


    @Bind(R.id.btn_back_dynamicDetails)
    ImageButton btnBackDynamicDetails;
    @Bind(R.id.details_dynamic_listView)
    RecyclerView detailsDynamicListView;
    @Bind(R.id.details_dynamic_editText)
    EditText detailsDynamicEditText;
    @Bind(R.id.details_dynamic_face)
    ImageView detailsDynamicFace;
    @Bind(R.id.details_dynamic_send)
    ImageView detailsDynamicSend;


    private int userID;
    private RecAdapter recAdapter = new RecAdapter();
    private List<TweetComment> commentList;
    private int commentCount;

    private RecyclerViewHeader header;
    private TextView nameTextView;
    private TextView commontTextView;
    private PhotoView pictureImageViewSmall;
    private TextView dataTextView;
    private CircleImageView circleHead;
    private PhotoView pictureImageViewBig;
    private Info info;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        userID = getIntent().getIntExtra("userID", 0);
        initView();
        initData();


        LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        detailsDynamicListView.setLayoutManager(llm);


        header = RecyclerViewHeader.fromXml(getApplicationContext(), R.layout.activity_details_dynamic_head);

        circleHead = (CircleImageView) header.findViewById(R.id.details_dynamic_head_image);
        nameTextView = (TextView) header.findViewById(R.id.details_dynamic_head_name);
        commontTextView = (TextView) header.findViewById(R.id.details_dynamic_head_comment);
        pictureImageViewSmall = (PhotoView) header.findViewById(R.id.details_dynamic_head_picture_small);
        pictureImageViewBig = (PhotoView) header.findViewById(R.id.details_dynamic_head_picture_big);
        dataTextView = (TextView) header.findViewById(R.id.details_dynamic_head_date);


        initHeadData();
        header.attachTo(detailsDynamicListView);

        initEvent();
    }


    private void initView() {
        setContentView(R.layout.activity_details_dynamic);
        ButterKnife.bind(this);
    }

    private void initData() {
        //网络请求
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.baseUrl)
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .build();
        RetrofitInterface reif = retrofit.create(RetrofitInterface.class);
        Call<TweetCommentList> call = reif.getTweetCommentList(0, 3, 20, userID);
        call.enqueue(new Callback<TweetCommentList>() {
            @Override
            public void onResponse(Call<TweetCommentList> call, Response<TweetCommentList> response) {
                commentList = response.body().getComments();
                commentCount = response.body().getPagesize();
                Log.d("DynamicDetailActivity", commentCount + "++++++++++++++++++++++++++++++++");

                detailsDynamicListView.setAdapter(recAdapter);
            }

            @Override
            public void onFailure(Call<TweetCommentList> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    private void initEvent() {
        pictureImageViewSmall.disenable();
        pictureImageViewSmall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pictureImageViewSmall.setVisibility(View.GONE);
                pictureImageViewBig.setVisibility(View.VISIBLE);
                info = pictureImageViewSmall.getInfo();
                pictureImageViewBig.animaFrom(info);
            }
        });
        pictureImageViewBig.enable();
        pictureImageViewBig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pictureImageViewBig.animaTo(info, new Runnable() {
                    @Override
                    public void run() {
                        pictureImageViewBig.setVisibility(View.GONE);
                        pictureImageViewSmall.setVisibility(View.VISIBLE);
                    }
                });
            }
        });
    }

    @OnClick(R.id.btn_back_dynamicDetails)
    public void onClick() {
        finish();
    }


    //RecyclerView适配器
    class RecAdapter extends RecyclerView.Adapter<RecHolder> {
        @Override
        public RecHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext())
                    .inflate(R.layout.activity_details_dynamic_item, parent, false);
            RecHolder recHolder = new RecHolder(view);
            return recHolder;
        }

        @Override
        public void onBindViewHolder(RecHolder holder, int position) {
            if (commentCount == 0) {
                holder.itemHeadImage.setVisibility(View.GONE);
                holder.itemName.setVisibility(View.GONE);
                holder.itemTime.setVisibility(View.GONE);
                holder.itemComment.setText("暂时无人评论");
            } else {
                Picasso.with(getApplicationContext()).load(commentList.get(position).getPortrait())
                        .resize(50, 50)
                        .centerCrop()
                        .into(holder.itemHeadImage);
                holder.itemComment.setText(commentList.get(position).getContent());
                holder.itemName.setText(commentList.get(position).getAuthor());
                holder.itemTime.setText(commentList.get(position).getPubDate());
            }
        }

        @Override
        public int getItemCount() {
            if (commentCount == 0) {
                return 1;
            } else {
                return commentCount;
            }
        }
    }

    class RecHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.item_headImage)
        CircleImageView itemHeadImage;
        @Bind(R.id.item_name)
        TextView itemName;
        @Bind(R.id.item_comment)
        TextView itemComment;
        @Bind(R.id.item_time)
        TextView itemTime;

        public RecHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //头部网络数据的获取
    private void initHeadData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.baseUrl)
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .build();
        RetrofitInterface reifHead = retrofit.create(RetrofitInterface.class);
        Call<TweetDetail> call = reifHead.getTweetDetailInfo(userID);
        call.enqueue(new Callback<TweetDetail>() {
            @Override
            public void onResponse(Call<TweetDetail> call, Response<TweetDetail> response) {
                nameTextView.setText(response.body().getTweet().getAuthor());
                Picasso.with(getApplicationContext()).load(response.body().getTweet().getPortrait()).into(circleHead);
                Picasso.with(getApplicationContext()).load(response.body().getTweet().getImgSmall()).into(pictureImageViewSmall);
                Picasso.with(getApplicationContext()).load(response.body().getTweet().getImgBig()).into(pictureImageViewBig);
                commontTextView.setText(response.body().getTweet().getBody());
                dataTextView.setText(response.body().getTweet().getPubDate());
            }

            @Override
            public void onFailure(Call<TweetDetail> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
